package com.wxg.thinkinginjava.file;

import org.junit.Ignore;
import org.junit.Test;


public class FileUtils {

	/**
	 * 
	 * @param clazz
	 * @return
	 */
	public static String getJavaFilePath(Class<?> clazz) {
		StringBuilder sbb = new StringBuilder();
		String rootpath = FileUtils.class.getClassLoader().getResource("").getPath();
		String pkg = clazz.getPackage().getName();
		//rootpath = rootpath.replaceAll("target[/\\\\]test-classes[/\\\\]", "");
		rootpath = rootpath.replaceAll("\\\\", "/");
		rootpath = rootpath.replaceFirst("/", "");
		rootpath = rootpath.replaceAll("target/test-classes/", "");
		sbb.append(rootpath).append("src/main/java/")//.append("src/test/java/")
			.append( pkg.replace(".", "/") )
			.append("/");
		rootpath = sbb.toString();
		return rootpath;
	}
	
	
	/**
	 * 
C:/wxg/git3/thinkinginjava/thinkinginjava/target/test-classes/
C:\wxg\git3\thinkinginjava\thinkinginjava\X.file
	 * @return
	 */
	public static String getRootpath() {
		String path = FileUtils.class.getClassLoader().getResource("").getPath();
		path = path.replaceAll("\\\\", "/");
		path = path.replaceAll("target/test-classes/", "");
		return path;
	}
	
	@Test
	public void test001() {
		String filepath = FileUtils.getJavaFilePath(FileUtils.class);
		System.out.println( String.format("filepath: %s", filepath) );
		System.out.println( "getRootpath: " + FileUtils.getRootpath() );
	}
	
	@Test
	@Ignore
	public void test002() {
		String str = "com.wxg.thinkinginjava.file";
		System.out.println( str.replace(".", "/") );
	}
	
}
